<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Project;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProjectController extends AdminController
{
    protected $title = '项目管理';
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Project(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('db_connection');
            $grid->column('db_host');
            $grid->column('db_port');
            $grid->column('db_database');
            $grid->column('prune_rule');
            $grid->column('delete_status')->display(function($delete_status){
                return $delete_status ? '正常' : '删除';
            });
            $grid->column('created_at');
            $grid->column('telescope')->display(function(){
                return '<a href="/telescope?projectId='.$this->id.'" target="_blank">查看面板</a>';
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
                $filter->between('created_at')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Project(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('db_connection');
            $show->field('db_host');
            $show->field('db_port');
            $show->field('db_database');
            $show->field('db_username');
            $show->field('db_password');
            $show->field('db_prefix');
            $show->field('prune_rule');
            $show->field('delete_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Project(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('db_connection')->default('mysql');
            $form->text('db_host')->default('127.0.0.1');
            $form->text('db_port')->default('3306');
            $form->text('db_database');
            $form->text('db_username');
            $form->text('db_password');
            $form->text('db_prefix');
            $form->text('prune_rule');
//            $form->text('delete_status');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
